package cn.huapu.power.energy.service.impl;

import cn.huapu.power.energy.bean.PeaUserpowerfee;
import cn.huapu.power.energy.bean.SysClient;
import cn.huapu.power.energy.bean.SysClientEng;
import cn.huapu.power.energy.bean.SysUser;
import cn.huapu.power.energy.ex.ErrorCodesException;
import cn.huapu.power.energy.mapper.PeaUserpowerfeeMapper;
import cn.huapu.power.energy.mapper.SysClientEngMapper;
import cn.huapu.power.energy.mapper.SysClientMapper;
import cn.huapu.power.energy.mapper.SysUserMapper;
import cn.huapu.power.energy.service.SysUserService;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.util.StringUtil;
import cn.huapu.power.energy.util.ValidateUtil;
import cn.huapu.power.energy.util.token.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 测试用户权限服务实现类
 * @author autogen
 *
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser,Integer> implements SysUserService {
    @Autowired
    private PeaUserpowerfeeMapper feeMapper;
    @Autowired
    private SysClientMapper sysClientMapper;
    @Autowired
    private SysClientEngMapper sysClientEngMapper;

    @Override
    @Transactional(readOnly = true)
    public SysUser queryByNamePs(String username, String password) {
        SysUser sysUser = mapper.queryByNamePs(username,password);
        return sysUser;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Integer> getRoleId(Integer pkid) {
    	List<Integer> roleids = mapper.getRoleId(pkid);
        return roleids;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    @Override
    public String registerApp(PeaUserpowerfee da, HttpSession session) {
        String token = null;
        //新增单位
        SysClient client = new SysClient();
        String clientname = da.getClientname();
        if(clientname.length()>2){
            clientname="**"+clientname.substring(2);
        }
        client.setClientnumber(da.getClientnumber());
        client.setName(clientname);
        client.setFullname(da.getClientname());        
        if(da.getAreano() == null || "".equals(da.getAreano())) {
        	client.setGvarid("500000000000");
        }else {
        	client.setGvarid(da.getAreano());
        }        
        client.setClientnumber(da.getClientnumber());
        sysClientMapper.insert(client);

        //新增用户
        String mobile = da.getMobile();
        SysUser sysUser = new SysUser();
        sysUser.setMobile(mobile);
        List<SysUser> list = mapper.list(sysUser);
        if(!ValidateUtil.listIsNull(list)){
            throw new ErrorCodesException("此用户已存在",ErrorCodesException.REG_ERR);
        }
        da.setUsercode(da.getMobile());
        da.setUsername(da.getMobile());
        da.setClientid(client.getPkid());
        int i = mapper.insertReg(da);

        //新增用户基本信息
        SysClientEng sysClientEng = new SysClientEng();
        sysClientEng.setClientid(client.getPkid());
        sysClientEng.setAreano(client.getGvarid());
        sysClientEng.setIdstytype(da.getIdstytype());
        sysClientEng.setVollevel(da.getVollevel());
        sysClientEng.setClientnumber(da.getClientnumber());
        sysClientEngMapper.insert(sysClientEng);
        token = "Bearer:"+ JwtUtil.getToken(da.getUsername(),String.valueOf(da.getPkid()),String.valueOf(client.getPkid()));
        return token;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
    public boolean updateUserApp(PeaUserpowerfee da) {
        int i = mapper.updateUserApp(da);
        SysClient sysClient = new SysClient();
        sysClient.setName(da.getClientname());
        sysClient.setFullname(da.getClientname());
        sysClient.setPkid(da.getClientid());
        sysClientMapper.update(sysClient);
        return true;
    }
}
